package mysql

import (
	"bluebell6/models"
	"github.com/jmoiron/sqlx"
	"strings"
)

// CreatePost创建帖子
func CreatePost(p *models.Post) (err error) {
	sqlStr := `insert into post (
    post_id,title,content,author_id,community_id)
    values (?,?,?,?,?)
    `
	_, err = db.Exec(sqlStr, p.ID, p.Title, p.Content, p.AuthorID, p.CommunityID)
	return
}

// GetPostById根据id查询单个帖子的数据
func GetPostById(pid int64) (post *models.Post, err error) {
	post = new(models.Post)
	sqlStr := `select
    post_id,title,content,author_id,community_id,create_time
    from post
    where post_id = ?
    `
	err = db.Get(post, sqlStr, pid)
	return

}

// GetPostList查询帖子列表的函数
func GetPostList(page, size int64) (posts []*models.Post, err error) {
	sqlStr := `select
    post_id,title,content,author_id,community_id,create_time
    from post
    ORDER BY create_time
    DESC
    limit ?,?
    `
	posts = make([]*models.Post, 0, 2) //不要写成make([]*models.Post,2)
	err = db.Select(&posts, sqlStr, (page-1)*size, size)
	return
}

// GetPostListByIDs根据给定id列表查询帖子数据
func GetPostListByIDs(ids []string) (postList []*models.Post, err error) {
	sqlStr := `select
    post_id,title,content,author_id,community_id,create_time
    from post
    where post_id in (?)
    order by FIND_IN_SET(post_id,?)
    `
	//https://www.liwenzhou.com/posts/Go/sqlx/
	query, args, err := sqlx.In(sqlStr, ids, strings.Join(ids, ","))
	if err != nil {
		return nil, err
	}
	query = db.Rebind(query)
	err = db.Select(&postList, query, args...) //!!!!
	return

}
